package zz.app.ustbgsmanager.other;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import zz.app.ustbgsmanager.R;

/**
 * Created by zz on 2018/6/11 21:39.
 */
public class ShortHorizontalDividerItemDecoration extends DividerItemDecoration {

    private final Context context;
    private int offset=50;

    public ShortHorizontalDividerItemDecoration(Context context) {
        super(context,DividerItemDecoration.VERTICAL);
        this.context=context;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        drawVertical(c,parent);
    }

    private void drawVertical(Canvas canvas,RecyclerView parent) {
        canvas.save();
        final int left;
        final int right;
        //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
        if (parent.getClipToPadding()) {
            left = parent.getPaddingLeft()+offset;
            right = parent.getWidth() - parent.getPaddingRight()-offset;
            canvas.clipRect(left, parent.getPaddingTop(), right,
                    parent.getHeight() - parent.getPaddingBottom());
        } else {
            left = offset;
            right = parent.getWidth()-offset;
        }

        final int childCount = parent.getChildCount();
        Rect bounds=new Rect();
        Drawable divider= context.getDrawable(R.drawable.drawable_divider);
        for (int i = 0; i < childCount-1; i++) {
            final View child = parent.getChildAt(i);
            parent.getDecoratedBoundsWithMargins(child, bounds);
            final int bottom = bounds.bottom + Math.round(child.getTranslationY());
            final int top;
            if (divider != null) {
                top = bottom - divider.getIntrinsicHeight();
                divider.setBounds(left, top, right, bottom);
                divider.draw(canvas);
            }

        }
        canvas.restore();
    }

}
